//------------------------------------------------------------------------------
#include <QtDebug>
#include "CMyTreeWidget.h"
#include "CMyTreeWidgetItemData.h" 
//------------------------------------------------------------------------------
QDataStream& operator << (QDataStream& out, const CMyTreeWidgetItemData& data) {
	
	out << QString::number(data.getId()) << QString::number(data.getType());
	
	return out;
}
//------------------------------------------------------------------------------
QDataStream& operator >> (QDataStream& in, CMyTreeWidgetItemData& data) {
	
	QString tmp;
	
	in >> tmp;
	data.setId(tmp.toInt());
	in >> tmp;
	data.setType((CMyTreeWidgetItemData::EDataType)tmp.toInt());
	
	return in;
}
//------------------------------------------------------------------------------
CMyTreeWidget::CMyTreeWidget(QWidget *parent) : QTreeWidget(parent) {
	qRegisterMetaType<CMyTreeWidgetItemData>("CMyTreeWidgetItemData");
	qRegisterMetaTypeStreamOperators<CMyTreeWidgetItemData>("CMyTreeWidgetItemData");
}
//------------------------------------------------------------------------------
void CMyTreeWidget::dragEnterEvent(QDragEnterEvent *event) {
	if(!event->mimeData()->hasFormat(CMyTreeWidgetItemData::mimeType())) {
		event->ignore();
		return;
	}
	
	QByteArray ba=event->mimeData()->data(CMyTreeWidgetItemData::mimeType());
	QDataStream ds(&ba, QIODevice::ReadWrite);
	CMyTreeWidgetItemData dataSrc;
	
	ds >> dataSrc;
	
	if(dataSrc.getType() == CMyTreeWidgetItemData::dtRoot) {
		event->ignore();
		return;
	}
	
	QTreeWidget::dragEnterEvent(event);
}
//------------------------------------------------------------------------------
void CMyTreeWidget::dragMoveEvent(QDragMoveEvent *event) {
	QPoint dropPoint=event->pos();
	QTreeWidgetItem *itemDest=itemAt(dropPoint);
	
	if(itemDest == 0) {
		event->ignore();
		return;
	}
	
	CMyTreeWidgetItemData data=qvariant_cast<CMyTreeWidgetItemData>(itemDest->data(0, Qt::UserRole));
	
	if(	(data.getType() != CMyTreeWidgetItemData::dtFolder &&
		data.getType() != CMyTreeWidgetItemData::dtRoot) || 
		!event->mimeData()->hasFormat(CMyTreeWidgetItemData::mimeType())) {
		event->ignore();
		return;
	}
	
	QTreeWidget::dragMoveEvent(event);
}
//------------------------------------------------------------------------------
void CMyTreeWidget::dropEvent(QDropEvent *event) {
	QPoint dropPoint=event->pos();
	QTreeWidgetItem *itemDest=itemAt(dropPoint);
	
	if(itemDest == 0) {
		event->ignore();
		return;
	}
	
	QByteArray ba=event->mimeData()->data(CMyTreeWidgetItemData::mimeType());
	QDataStream ds(&ba, QIODevice::ReadWrite);
	CMyTreeWidgetItemData dataSrc;
	CMyTreeWidgetItemData dataDst=qvariant_cast<CMyTreeWidgetItemData>(itemDest->data(0, Qt::UserRole));;
	
	ds >> dataSrc;
	
	QTreeWidget::dropEvent(event);
	
	emit itemDroped(dataSrc.getId(), dataDst.getId());
}
//------------------------------------------------------------------------------
QStringList CMyTreeWidget::mimeTypes(void) const {
	QStringList mimeTypes;
	
	mimeTypes << CMyTreeWidgetItemData::mimeType();
	
	return mimeTypes;
}
//------------------------------------------------------------------------------
QMimeData * CMyTreeWidget::mimeData(const QList<QTreeWidgetItem *> items) const {
	QMimeData *mimeData=0;
	
	if(items.size() == 1) {
		mimeData=new QMimeData();
		QByteArray ba;
		CMyTreeWidgetItemData data=qvariant_cast<CMyTreeWidgetItemData>(items.at(0)->data(0, Qt::UserRole));
		QDataStream ds(&ba, QIODevice::ReadWrite);
		ds << data;
		
		mimeData->setData(CMyTreeWidgetItemData::mimeType(), ba);
	}
	
	return mimeData;
}
//------------------------------------------------------------------------------